package com.stox.core.dao;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.stox.core.manager.InstallationManager;
import com.stox.core.manager.ScripManager;
import com.stox.core.model.Bar;
import com.stox.core.model.IBar;
import com.stox.core.model.Scrip;
import com.stox.core.util.Constant;
import com.stox.ioc.Container;

public class BarDao {

	private String fileBasePath = "D:/Users/paralp/Documents/temp/stox/1.0/filebase";
	private FileTemplate fileTemplate;
	private ScripManager scripManager;
	
	public BarDao() {
		InstallationManager installationManager = Container.get(InstallationManager.class);
		this.fileBasePath = installationManager.getFilebasePath();
		this.fileTemplate = Container.get(FileTemplate.class);
		this.scripManager = Container.get(ScripManager.class);
	}
	
	private String createFilePath(String scripCode, String exchangeCode){
		return fileBasePath+
		File.separator+"data"+
		File.separator+exchangeCode+
		File.separator+scripCode+".txt";
	}
	
	public List<IBar> get(String scripCode, String exchangeCode) throws Exception{
		String string = fileTemplate.read(createFilePath(scripCode, exchangeCode));
		Scrip scrip = scripManager.findByCode(scripCode);
		List<IBar> bars = new ArrayList<IBar>();
		String[] tokens = string.split(Constant.lineSeparator);
		if(tokens != null && tokens.length > 0){
			for(int i = 0; i < tokens.length; i++){
				Bar bar = Bar.fromString(tokens[i], scripCode);
				if(bar != null){
					bar.setScrip(scrip);
					bars.add(bar);
				}
			}
		}
		Collections.sort(bars);
		return bars;
	}
	
	public List<Bar> get(String scripCode, String exchangeCode, int count) throws Exception{
		String string = fileTemplate.read(createFilePath(scripCode, exchangeCode), count);
		Scrip scrip = scripManager.findByCode(scripCode);
		List<Bar> bars = new ArrayList<Bar>();
		String[] tokens = string.split(Constant.lineSeparator);
		if(tokens != null && tokens.length > 0){
			for(int i = 0; i < tokens.length; i++){
				Bar bar = Bar.fromString(tokens[i], scripCode);
				if(bar != null){
					bar.setScrip(scrip);
					bars.add(bar);
				}
			}
		}
		Collections.sort(bars);
		return bars;
	}
	
	public void persistBatch(List<Bar> bars, String scripCode, String exchangeCode){
		fileTemplate.write(bars, createFilePath(scripCode, exchangeCode));
	}
	
	public void persist(List<Bar> bars) throws Exception{
		if(bars != null && !bars.isEmpty()){
			ExecutorService executor = Executors.newFixedThreadPool(10);
			for(final Bar bar : bars){
				 executor.execute(new Runnable(){
					 @Override
					public void run() {
						 try{
							 persist(bar);
						 }catch(Exception e){
							 e.printStackTrace();
						 }
						}
					 });
			}
			executor.shutdown();
			while (!executor.isTerminated()) {
				Thread.yield();
			}
		}
	}

	public void persist(Bar bar) throws Exception{
		scripManager.persist(bar.getScrip());
		fileTemplate.write(bar, createFilePath(bar.getScripCode(), bar.getScrip().getExchangeCode()));
	}
}
